<?php

namespace App\Hospital\Controllers;

use App\Admin\Actions\NoticeAction;
use App\Hospital\Repositories\Article;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\Notice;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class NoticeController extends AdminController
{

    protected function title()
    {
        $this->title = '通知';

        return $this->title;
    }
    protected function description()
    {
        $this->description['index'] = '添加通知后点击操作->推送->选择通知人群确定推送（如签约人数较多推送会较慢）';

        return $this->description;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(['notice','info']), function (Grid $grid) {
            $grid->model()->where('type',3)->where('hospital_id',Admin::user()->hospital_id)->orderby('id','desc');
            $grid->column('info.title','通知标题');
            $grid->column('notice.type','推送人群')->using(Notice::$typeTxt);
            $grid->column('notice.status','推送状态')->using(Notice::$statusTxt);
            $grid->column('notice.created_at','推送时间');

            $grid->disableBatchDelete();//禁用批量操作
            $grid->disableRowSelector();//禁用行选择器

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if($this->hospital_id != Admin::user()->hospital_id){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableQuickEdit();
                }
            });

            $grid->column('created_at');


            $grid->actions(function ($actions) {
                if(!$this->notice){
                    $actions->append(new NoticeAction());
                    $actions->disableDelete();
                }
            });


            $grid->filter(function($filter){
                // 禁止自动展开过滤器
                $filter->expand(false);
                $filter->panel();
                $filter->like('info.title', '文章标题');
                $filter->like('info.content', '文章内容');
                //$filter->like('type', '文章内容');
                //$filter->in('id')->multipleSelect(); // 设置编辑数据显示
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(['info','FamilyType']), function (Show $show) {

            $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });
            $show->field('info.title','通知标题');
            $show->field('info.ftitle','通知副标题');
            $show->field('info.content','通知内容')->unescape()->as(function ($avatar) {

                return $avatar;

            });

            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $hospital = Hospital::query()->where('id',Admin::user()->hospital_id)->first();
        return Form::make(Article::with(['info','FamilyType']), function (Form $form) use($hospital) {
            $form->text('info.title','通知标题');
            $form->text('info.ftitle','通知副标题');
            $form->editor('info.content','通知内容');
            $form->hidden('info.source')->value($hospital->name);
            //$form->multipleSelect('familyType','文章对应人群')->options(FamilyType::query()->pluck('name','id'));
            $form->hidden('type')->value(3);
            $form->hidden('hospital_id')->value(Admin::user()->hospital_id);
            $form->hidden('status')->value(1);//社区通知无需审核
        });
    }
}
